package com.aaa.service.impl;

import com.aaa.entity.EduSubject;
import com.aaa.dao.EduSubjectMapper;
import com.aaa.service.IEduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyd
 * @since 2021-03-24
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements IEduSubjectService {
@Resource
private  EduSubjectMapper mapper;
    @Override
    public List<EduSubject> queryAll(EduSubject subject) {
        List<EduSubject> ma = new ArrayList<>();
        if (subject.getTitle() == null || subject.getTitle().equals("")) {
            subject.setParentId(0);
            ma = mapper.queryAll(subject);
            for (EduSubject record : ma) {
                subject.setParentId(record.getId());
                record.setChildren(mapper.queryAll(subject));
            }
        } else {
            List<EduSubject> maps = mapper.queryAll(subject);
            if (maps.size() > 0) {
                for (EduSubject sub : maps) {
                    if (sub.getParentId() == 0) {
                        if (ma.size()>0) {
                            for (EduSubject eduSubject : ma) {
                                if (eduSubject.getId() == sub.getId()) {

                                } else {
                                    ma.add(sub);
                                }
                            }
                        } else {
                            ma.add(sub);
                        }


                    } else {
                        EduSubject ersub = new EduSubject();
                        ersub.setId(sub.getParentId());
                        List<EduSubject> eduSubjects = mapper.queryAll(ersub);
                        EduSubject eduSubject = eduSubjects.get(0);

                        if (ma.size() > 0) {
                            for (EduSubject edusub : ma) {
                                if (edusub.getId() == eduSubject.getId()) {
                                    List<EduSubject> children = edusub.getChildren();
                                    children.add(sub);
                                    edusub.setChildren(children);
                                }
                            }
                        } else {
                            List<EduSubject> subs = new ArrayList<>();
                            subs.add(sub);
                            eduSubject.setChildren(subs);
                            ma.add(eduSubject);
                        }
                    }
                }
            }
        }

        return ma;
    }
}
